//
//  MyVideoViewController.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/3/5.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "MyVideoViewController.h"
#import "UIViewController+AlertViewAndActionSheet.h"
#import "VideoConfigureViewController.h"
#import "VideoRecordViewController.h"
#import "CYImagePickerViewController.h"
@interface MyVideoViewController ()<TZImagePickerControllerDelegate>
@property(nonatomic, strong) QMUIPopupMenuView *popupView2;

@end

@implementation MyVideoViewController

- (void)viewDidLoad {
    self.title = ASLocalizedString(@"小视频");
    [super viewDidLoad];
    [self addNavigationItemWithTitles:@[ASLocalizedString(@"添加小视频   ")] isLeft:NO target:self action:@selector(addVideo)
                                 tags:@[@4000]];
    self.logic.getMyVideo = YES;
    
    [self.logic loadData];
}

//跳转到录制视频
-(void)jumpshootVideo
{
    VideoConfigure* _videoConfig = [[VideoConfigure alloc] init];
    
    VideoRecordViewController *vc = [[VideoRecordViewController alloc] initWithConfigure:_videoConfig];
    [self.navigationController pushViewController:vc animated:YES];
    //    [APP_DELEGATE pushViewController:vc];
}

- (void)addVideo {
    QMUIAlertController *alertMore = [[QMUIAlertController alloc] initWithTitle:ASLocalizedString(@"操作")message:@"" preferredStyle:QMUIAlertControllerStyleActionSheet];
    
    [alertMore addAction:[QMUIAlertAction actionWithTitle:ASLocalizedString(@"拍摄")style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
        
        [self jumpshootVideo];
    }]];
    
    [alertMore addAction:[QMUIAlertAction actionWithTitle:ASLocalizedString(@"上传本地视频")style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
        [self selectVideo];
    }]];
    
    [alertMore addAction:[QMUIAlertAction actionWithTitle:ASLocalizedString(@"取消")style:QMUIAlertActionStyleCancel handler:^(QMUIAlertAction *action) {
        
    }]];
    
    [alertMore showWithAnimated:YES];
}


-(void)selectVideo
{
    CYImagePickerViewController *imagePickerVc = [[CYImagePickerViewController alloc] initWithMaxImagesCount:1 delegate:self];
    [imagePickerVc.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    imagePickerVc.naviBgColor = [UIColor blackColor];
    
    __weak __typeof(self) weakSelf = self;
    
    [imagePickerVc setDidFinishPickingVideoHandle:^(UIImage *coverImage, id asset) {
        if(![asset isKindOfClass:[PHAsset class]])
        {
            [[HUDHelper sharedInstance] tipMessage:ASLocalizedString(@"手机系统版本较低!")];
            return;
        }
        
        [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset * avasset, AVAudioMix * audioMix, NSDictionary * info) {
            dispatch_async(dispatch_get_main_queue(), ^{
                AVURLAsset *urlAsset = (AVURLAsset *)avasset;
                
                VideoEditViewController *editVideo = [[VideoEditViewController alloc] init];
                [editVideo setVideoPath:[urlAsset.URL absoluteString]];
                //                editVideo.videoAsset = avasset;
                [kAppDelegate pushViewController:editVideo];
                
                //                [weakSelf doSomethingWithAsset:avasset];
            });
        }];
        
    }];
    imagePickerVc.allowPickingOriginalPhoto = NO;
    imagePickerVc.allowPickingGif = NO;
    imagePickerVc.allowPickingImage = NO;
    imagePickerVc.naviTitleColor = [UIColor blackColor];
    imagePickerVc.barItemTextColor =  [UIColor blackColor];
    imagePickerVc.oKButtonTitleColorNormal = MainColor;
    imagePickerVc.showSelectBtn = YES;
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}


//*******重写的时候需要走一句话
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectIndexPath = indexPath;
    videoListModel *model = self.logic.dataArray[indexPath.row];
    
    QMUIAlertController *alertMore = [[QMUIAlertController alloc] initWithTitle:ASLocalizedString(@"操作")message:@"" preferredStyle:QMUIAlertControllerStyleActionSheet];
    
    [alertMore addAction:[QMUIAlertAction actionWithTitle:ASLocalizedString(@"查看")style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
        [self goVideoViewWithModel:model];
    }]];
    
    [alertMore addAction:[QMUIAlertAction actionWithTitle:ASLocalizedString(@"删除")style:QMUIAlertActionStyleDestructive handler:^(QMUIAlertAction *action) {
        
        NSString *buyTip = [NSString stringWithFormat:ASLocalizedString(@"确定删除此视频？")];
        QMUIAlertController *alert = [[QMUIAlertController alloc] initWithTitle:ASLocalizedString(@"提示")message:buyTip preferredStyle:QMUIAlertControllerStyleAlert];
        [alert addAction:[QMUIAlertAction actionWithTitle:ASLocalizedString(@"取消")style:QMUIAlertActionStyleCancel handler:^(QMUIAlertAction *action) {
            
        }]];
        
        [alert addAction:[QMUIAlertAction actionWithTitle:ASLocalizedString(@"确定")style:QMUIAlertActionStyleDestructive handler:^(QMUIAlertAction *action) {
            [self delVideoWithModel:model];
        }]];
        [alert showWithAnimated:YES];
        
        
    }]];
    
    [alertMore addAction:[QMUIAlertAction actionWithTitle:ASLocalizedString(@"取消")style:QMUIAlertActionStyleCancel handler:^(QMUIAlertAction *action) {
        
    }]];
    
    [alertMore showWithAnimated:YES];
}

-(void)delVideoWithModel:(videoListModel *)model
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD showTopTipMessage:@"删除成功"];
        [self.logic loadData];
    });
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"short_video_api" A:@"del_video"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:StringFromInt(model.id) forKey:@"video_id"];
    
    [CYNET POSTv2:url parameters:param responseCache:^(id responseObject) {
        
    } success:^(id responseObject) {
        [self.logic loadData];
    } failure:^(NSString *error) {
        [self.logic loadData];
    } hasCache:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ————— 上拉刷新 —————
-(void)footerRereshing{
    self.logic.page+=1;
    [self.logic loadData];
    [self removeNoDataImage];
}

#pragma mark ————— 数据拉取完成 渲染页面 —————
-(void)requestDataCompleted{
    [self.collectionView.mj_footer endRefreshing];
    [self.collectionView.mj_header endRefreshing];
    
    [UIView performWithoutAnimation:^{
        [self.collectionView reloadData];
    }];
    
}

#pragma mark ————— 下拉刷新 —————
-(void)headerRereshing{
    self.logic.page = 1;
    [self.logic loadData];
    [self removeNoDataImage];
    
}

@end
